<?php

namespace App\Models;

use EasySwoole\ORM\AbstractModel;

class Base extends AbstractModel
{

    const PAGE_NO = 1;
    const PAGE_SIZE = 10;

    ###############     默认参数      #################

    protected static $defaultField = '*';
    protected static $listField = '';
    protected static $infoField = '';

    ##################################################

    protected static function redis(array $config=[])
    {
        $instance = \EasySwoole\EasySwoole\Config::getInstance();
        $config = !empty($config) ? $config : $instance->getConf('REDIS');
        $redis = new \EasySwoole\Redis\Redis(new \EasySwoole\Redis\Config\RedisConfig($config));
        return $redis;
    }

    /**
     * 验证分页数据
     */
    protected static function checkPageInfo(&$pageNo, &$pageSize)
    {
        if ($pageNo <= 0) $pageNo = self::PAGE_NO;
        if ($pageSize <= 0) $pageSize = self::PAGE_SIZE;
    }

    /**
     * 生成当前分页信息
     */
    protected static function createPageInfo($count, $pageNo, $pageSize)
    {
        return [  
            'count'         => $count,
            'pages'         => ceil($count/$pageSize),
            'current_page'  => $pageNo,
            'size'          => $pageSize
        ];
    }

    /**
     * 生成列表分页信息
     */
    protected static function createListInfo($list, $count, $pageNo, $pageSize)
    {
        $pageInfo = [  
            'count'         => $count,
            'pages'         => ceil($count/$pageSize),
            'current_page'  => $pageNo,
            'size'          => $pageSize
        ];
        return ['list' => $list, 'page_info' => $pageInfo];
    }

    /**
     * 详情
     */
    public function find(array $where)
    {
        $result = self::create() -> field(!empty(static::$infoField) ? static::$infoField : static::$defaultField) -> where($where) -> get();
        if ($result) return $result -> toArray();
        return null;
    }

    /**
     * 添加
     */
    public function add(array $data)
    {
        return self::create() -> data($data) -> save();
    }
    
    /**
     * 分页列表
     */
    public function list(array $param) : ? array
    {
        $pageNo = $param['page_no'] ?? 0;
        $pageSize = $param['page_size'] ?? 0;
        self::checkPageInfo($pageNo, $pageSize);

        $where = [];
        $model = self::create()
            -> field(!empty(static::$listField) ? static::$listField : static::$defaultField)
            -> where($where)
            -> limit(($pageNo - 1) * $pageSize, $pageSize)
            -> withTotalCount();
        
        $model -> all();
        
        $list = $model -> lastQueryResult() -> getResult();
        if (!$list) return null;
        $count = $model -> lastQueryResult() -> getTotalCount();
        
        return self::createListInfo($list, $count, $pageNo, $pageSize);
    }

    /**
     * 所有列表
     */
    public function allList($param) : ? array
    {
        $field = $param['field'] ?? (!empty(static::$listField) ? static::$listField : static::$defaultField);
        $where = $param['where'] ?? [];
        $model = self::create() -> field($field) -> where($where);
        $model -> all();
        $list = $model -> lastQueryResult() -> getResult();
        return $list ?? null;
    }

}
